#include "Line.h"
#include <cmath>

Line::Line() : x1(), y1(), x2(), y2(), m()
{
}

Line::Line(float x1, float y1, float x2, float y2) : x1(x1), y1(y1), x2(x2), y2(y2), m((y1 + y2) / (x1 + x2))
{
}

float Line::getX1() const
{
	return x1;
}

float Line::getY1() const
{
	return y1;
}

float Line::getX2() const
{
	return x2;
}

float Line::getY2() const
{
	return y2;
}

void Line::setX1(float x1)
{
	this->x1 = x1;
}

void Line::setY1(float y1)
{
	this->y1 = y1;
}

void Line::setX2(float x2)
{
	this->x2 = x2;
}

void Line::setY2(float y2)
{
	this->y2 = y2;
}

Point Line::intersectsWall(const Line &wall)
{
	float b1;
	Point p;

	b1 = (x1 - wall.x1) * wall.m + (y1 - wall.y1);
	p.x = b1 / (m - wall.m);
	p.y = wall.y1 + (x1 - wall.x1) * m;

	return p;
}

void Line::draw()
{
	float x = x1, y = y1;

	while (x != x2 && y != y2)
	{
		glPushMatrix();
		glTranslatef(x, 0, y);
		glColor3f(1.0f, 0.0f, 0.0f);
		glRotatef(m*360, 0.0f, 1.0f, 0.0f);
		glBegin(GL_QUADS);
			glVertex3f(-1.0f, 1.0f, 0.0f);
			glVertex3f(1.0f, 1.0f, 0.0f);
			glVertex3f(1.0f,-1.0f, 0.0f);
			glVertex3f(-1.0f,-1.0f, 0.0f);
		glEnd();
		glPopMatrix();

		if (x < x2)
			x++;
		if (y < y2)
			y++;
	}
}